class RespBen {
  async respBean(ctx, next) {
    /**
     * 成功时的响应
     * @param {*} code
     * @param {*} msg
     * @param {*} data
     * @param {*} httpStatus
     */
    ctx.success = function (
      code = 200,
      msg = "请求成功",
      data,
      httpStatus = 200
    ) {
      ctx.status = httpStatus;
      ctx.type = "json";
      ctx.body = {
        code,
        msg,
        data,
      };
    };
    /**
     * 失败时的响应
     * @param {*} code
     * @param {*} msg
     * @param {*} httpStatus
     */
    ctx.failure = function (code = 400, msg = "请求失败", httpStatus = 400) {
      ctx.status = httpStatus;
      ctx.type = "json";
      ctx.body = {
        code,
        msg,
      };
    };
    await next();
  }
}

module.exports = new RespBen();
